package com.example.work_notebook;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class EditActivity extends AppCompatActivity {
    private EditText title;
    private Button save;
    private EditText dada;
    private String id;
    private Button delete;
    private ImageView editImage;
    private VideoView editVideo;
    private TextView editLocation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        title = findViewById(R.id.edit_title);
        save = findViewById(R.id.edit_save);
        dada = findViewById(R.id.edit_data);
        delete = findViewById(R.id.edit_delete);
        editImage = findViewById(R.id.edit_image);
        editLocation = findViewById(R.id.edit_location);
        editVideo = findViewById(R.id.edit_video);
        Intent intent = getIntent();
      String title1 = intent.getStringExtra("TITLE");
      String data1 = intent.getStringExtra("DATA");
      id = intent.getStringExtra("ID");
      title.setText(title1);
      dada.setText(data1);
      save.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
           notebook notebook = new notebook();
           notebook.setTitle(title.getText().toString());
           notebook.setContent(dada.getText().toString());
           notebook.update(id, new UpdateListener() {
               @Override
               public void done(BmobException e) {
                   if(e==null){
                       Toast.makeText(EditActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                       Intent intent = new Intent(EditActivity.this,MainActivity.class);
                       startActivity(intent);
                   }else {
                       Log.e("EditActivity","保存出错");
                   }
               }
           });
          }
      });
      delete.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              notebook note = new notebook();
              note.setObjectId(id);
              note.delete(new UpdateListener() {
                  @Override
                  public void done(BmobException e) {
                      if(e==null){
                          Toast.makeText(EditActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                          Intent intent = new Intent(EditActivity.this,MainActivity.class);
                          startActivity(intent);
                      }else{
                          Log.e("EditActivity","删除失败");
                      }
                  }
              });
          }
      });
    }
}
